<?php

require_once( '../system/smarty.inc' );

require_once( '../system/mdao/Class_M_AJAREKS.php' );
require_once( '../system/mdao/Class_M_OTHER.php' );

require_once( '../system/Class_ERROR.php' );
require_once( '../system/login.inc.php' );
require_once( '../system/aoki_m.php' );
require_once( '../system/Class_PWD.php' );

Class_PWD::valid_auth(AOKI_AUTH_AJAREKS,$login['staff_auth']);

$dosearch = FALSE ;
$dao = new Class_M_AJAREKS;
//$zone = Class_M_OTHER::get_zone_array();

if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ||
	 isset( $_GET['hist'] ) &&  (  $_GET['hist'] == "2" ) ) {
	$forms =  $_SESSION['MSEARCH']['M_AJAREKS'];
}else {
	$forms = $_GET;
	$_SESSION['MSEARCH']['M_AJAREKS'] = $forms;
}

$wherearr =  array();
$whereor =  array();
foreach ($aoki_area_zone as $k=>$v) {
	$whereor[] = sprintf(" auth =  '%s' ",  $k );

}
$wherearr[] = "(".implode(' OR ', $whereor).")";


if ( isset( $forms['auth'] ) &&  ( !empty( $forms['auth'] ) ) ){
	$wherearr[] = sprintf(" a.auth =  '%s' ",  $forms['auth'] );
}
if ( isset( $forms['user_name'] ) &&  ( !empty( $forms['user_name'] ) ) ){
	$wherearr[] = sprintf(" a.user_name like  '%s' ",  "%".$forms['user_name']."%" );
}
if ( isset( $forms['upd_f'] ) &&  ( !empty( $forms['upd_f'] ) ) ){
	$wherearr[] = str_replace( "u_time", "b.u_time", Class_PWD::get_u_time_where_sql($forms['upd_f'], 1 ) );
}

if ( isset( $forms['upd_t'] ) &&  ( !empty( $forms['upd_t'] ) ) ){
	$wherearr[] = str_replace( "u_time", "b.u_time", Class_PWD::get_u_time_where_sql($forms['upd_t'], 2 ) );
}

$shop_name = NULL;
if ( isset( $forms['shop_name'] ) &&  ( !empty( $forms['shop_name'] ) ) ){
	$shop_name =  $forms['shop_name'] ;
}

//---CSVダウンロード　処理開始

if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ){
	$_GET["csv"] = "";
	$tmpfilename = "ajareks".date("Ymd_His");
	header("Content-Type: application/octet-stream"); 
	header("Content-Disposition: attachment; filename=$tmpfilename.csv");
	$page = NULL ;
	$data = $dao->searchcsv( $wherearr, $page, $shop_name);
	$colums = array();
	
	//列名
	$col_heads = array(
		'login_id'        => array( 'ログインID',  1),
		'user_name'       => array( 'ユーザ名',    1),
		'auth'            => array( '区分',        1),	
		//'zone_name'       => array( 'ゾーン名',    1),
		'shop_cd'         => array( '店舗コード',  1),
		'shop_name'       => array( '店舗名',      1),			
		'u_time'          => array( '更新日時',    3),
		'c_time'          => array( '登録日時',    3),
		'm_user_name'     => array( '更新者',      1)
	);

	//列名
	foreach ($col_heads as $k=>$v) {
		$colums["name"][]   = $k;
		$colums["head"][]   = $v[0];
		$colums["type"][$k] = $v[1];
	}
	
	//データ整形開始
	foreach ($data as $k => $v) {
		$data[$k]["auth"]        =  $aoki_area_zone[$v['auth']];
		//$data[$k]["zone_name"]   =  $zone[$v['zone_cd']];
		$data[$k]["c_time"]      =  Class_PWD::dateTime_to_YmdHi($v["c_time"]);
		$data[$k]["u_time"]      =  Class_PWD::dateTime_to_YmdHi($v["u_time"]);
	}
	//データ整形完了
	$dao->output_csv($data, $colums);
	exit;

}

//---CSVダウンロード　処理完了

if ( isset( $forms['search'] ) && (  !empty( $forms['search'] ) ) ){
	
	$dosearch = TRUE;
}

if( isset( $forms['page'] ) && is_numeric( $forms['page'] ) ){
	$dosearch = TRUE;
	$page['current'] = $forms['page'];
}else{
	$page['current'] = 1;
}

$page['item'] = PAGE_MAX;

if ($dosearch) {
	//$page内容は関数内部更新されます。
	$data = $dao->search( $wherearr, $page, $shop_name);
}


//表示ために整形開始

$disp = array();



//表示ために整形完了


if( isset($forms) ) $smarty->assign('forms', $forms);
if( isset($data) )  $smarty->assign('data', $data);
//if( isset($disp) )  $smarty->assign('disp', $disp);
if( isset($page) )  $smarty->assign('page', $page);

/*
$block = Class_M_OTHER::get_block_array( PLEASE_SELECT_ARR );
$smarty->assign('block', $block );

$local= Class_M_OTHER::get_local_array( PLEASE_SELECT_ARR );
$smarty->assign('local', $local );
*/

$flags[""] = "すべて";
foreach ($aoki_area_zone as $k => $v) {
	$flags[$k] = $v;
}

$smarty->assign('flags', $flags );
$smarty->assign('menu', $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );
$smarty->assign('pageing', $smarty->fetch('page.html') );
$smarty->display('majareks_index.html');
?>